// 云函数入口文件
const cloud = require('wx-server-sdk')
const moment = require("moment");

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let { OPENID } = wxContext
  let { result, nickName, avatarUrl } = event
  var success = false
  var message = "活动签到失败，请联系教练手工签到"
  var resp = {}
  const db = cloud.database()
  const _ = db.command

  console.log('result',result)
  var res = await db.collection('activity').doc(result).get()
  console.log('res',res)

  if (!res.data) {
    success = false
    message = "没有这个活动哦"
  } else {
    let activity = res.data
    moment.locale('zh-cn');
    let now = moment().add(8, 'hours').valueOf();
    let start = moment(activity.beginTime, 'YYYY/MM/DD hh:mm').valueOf();
    let end = moment(activity.endTime, 'YYYY/MM/DD hh:mm').valueOf();
    if (now >= start && now <= end) {
      var res = await db.collection('registration-form').where({
        activityId: activity._id,
        _openid: OPENID
      }).get()
      if (res.data.length > 0) {
        success = false
        message = "请勿重复签到"
      } else {
        var res = await db.collection('registration-form').add({
          data: {
            _openid: OPENID,
            nickName: nickName,
            avaterUrl: avatarUrl,
            activityName: activity.name,
            activityId: activity._id,
            activityTime: activity.beginTime,
            point:activity.point
          }
        })

        // let user = await db.collection('user').where({
        //   _openid: OPENID
        // }).get()
    
        // let point = user.data[0].point + activity.point;
        // await db.collection('user').where({
        //   _openid: OPENID
        // }).update({
        //   data: {
        //     point: point
        //   }
        // })

        // var userData = await db.collection('userInfo').where({
        //   _openid: OPENID
        // }).get()
        // let userInfo = userData.data[0]
        // if (userInfo) {
        //   await db.collection('userInfo').where({
        //     _openid: OPENID
        //   }).update({
        //     data: {
        //       point: point,
        //     },
        //   })
        // } else {
        //   await db.collection('userInfo').add({
        //     data: {
        //       _openid: OPENID,
        //       nickName: nickName,
        //       point: activity.point,
        //       distance: 0,
        //       calorie: 0,
        //       time: 0
        //     },
        //   })
        // }
        moment.locale('zh-cn');
        date = moment().add(8, 'hours').format('lll');
        systemTime = moment().valueOf();
        var card = {
          _openid: OPENID,
          type: {
            index:5,
            name:'周训'
          },
          distance: 0,
          time: 90,
          speed: 0,
          message: activity.name,
          level: 0,
          ropeNo: 0,
          heartrate: 0,
          calorie: 0,
          images: [],
          status: 'unChecked',
          date: date,
          systemTime: systemTime,
          point: activity.point,
          altitude: 0,
          userInfo: {
            nickName: nickName,
            avatarUrl: avatarUrl,
          }
        }
        await db.collection('cards').add({
          data: card,
          success: function (res) {
            id = res._id
            console.log(res)
          }
        })

        success = true
        message = "签到成功"
      }

    } else {
      success = false
      message = "不在活动时间范围内..."
    }
  }

  return {
    success: success,
    message: message,
    data: resp
  }
}